package com.gmrz.uap.cfg;

import org.apache.log4j.Logger;
import org.quartz.utils.ConnectionProvider;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class UapJobConnectionProvider implements ConnectionProvider {

    private static final Logger LOG = Logger.getLogger(UapJobConnectionProvider.class);

    private DataSource datasource;

    public Connection getConnection() throws SQLException {

        Connection connection = datasource.getConnection();
        //connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        return connection;
    }

    public void shutdown() throws SQLException {
        datasource = null;
    }

    public void initialize() throws SQLException {
        try {
            datasource = (DataSource) new InitialContext().lookup("java:comp/env/" + SystemConst.DB_JNDI_NAME);
        } catch (NamingException e) {
            LOG.error("获取不到" + SystemConst.DB_JNDI_NAME + "的JNDI名称",e);
        }
    }

    public DataSource getDatasource() {
        return datasource;
    }

    public void setDatasource(DataSource datasource) {
        this.datasource = datasource;
    }
}
